//
//  DLBaseWebVC.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/22.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLBaseWebVC.h"



@interface DLBaseWebVC()<UIWebViewDelegate>


@end

@implementation DLBaseWebVC
- (void) webViewDidFinishLoad:(UIWebView *)webView{
    
    NSLog(@"webViewDidFinishLoad");
    self.context = [_baseWeb valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.context[@"appToJs"] = ^(id result) {
        
        NSLog(@"webView页面，goToApp返回的json是：%@", result);
        
//        NSDictionary *dic = result;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
        });
    };
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    NSString* reqUrl = request.URL.absoluteString;
    NSLog(@"webView的链接是：%@", reqUrl);
    return YES;
}

- (void) viewDidLoad {
    [super viewDidLoad];
    
    [self setUpNav];
    [self layoutSubControls];
}
- (void) layoutSubControls {
    UIWebView *baseWeb = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenSafeAreaHeight - kHeightNavigation)];
    baseWeb.delegate = self;
    [self.view addSubview:baseWeb];
    self.baseWeb = baseWeb;
}
- (void) setUpNav {
    self.titleView.title = self.titleString;
}

- (void)backPreviousPage {
    if (self.baseWeb.canGoBack) {
        [self.baseWeb goBack];
    }else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
}
- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self cleanCacheAndCookie];
}

/**清除缓存和cookie*/
- (void)cleanCacheAndCookie{
    //清除cookies
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]){
        [storage deleteCookie:cookie];
    }
    //清除UIWebView的缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    NSURLCache * cache = [NSURLCache sharedURLCache];
    [cache removeAllCachedResponses];
    [cache setDiskCapacity:0];
    [cache setMemoryCapacity:0];
}
@end
